package sel.me.spring.boot159.web.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * @author shenenlu 2021年04月21日 下午14:26:41
 */
@Configuration
public class RestTemplateConfig {

    @Bean("eurekaRestTemplate")
    @LoadBalanced
    public RestTemplate eurekaRestTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        System.out.println(restTemplate.hashCode());
        return restTemplate;
    }


    @Bean("outerRestTemplate")
    public RestTemplate outerRestTemplate(@Qualifier("shortClientHttpRequestFactory") ClientHttpRequestFactory shortClientHttpRequestFactory){
        RestTemplate restTemplate = new RestTemplate(shortClientHttpRequestFactory);
        System.out.println(restTemplate.hashCode());
        return restTemplate;
    }

    @Bean("shortClientHttpRequestFactory")
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(5000);
        factory.setReadTimeout(15000);
        return factory;
    }


}
